package com.baojiaren.service.impl;

import com.baojiaren.pojo.Order;
import com.baojiaren.pojo.Product;
import com.baojiaren.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author ✎ℳ๓₯㎕.倾心❀、
 * @create 2021-07-15-10:16
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient; //元数据对象
    @Autowired
    private LoadBalancerClient loadBalancerClient; // Ribbon 负载均衡器
    @Override
    public Order selectOrderById(Integer id) {
        return new Order(id,"order-001","中国",3199D,selectProductListByLoadBalancerClient());
    }

    /**
     * 第一种元数据方法
     * @return
     */
    private List<Product> selectProductListByDiscovery(){
        StringBuffer sb=null;
        //获取服务列表
        List<String> serviceIds=discoveryClient.getServices();
        if (CollectionUtils.isEmpty(serviceIds)){
            return null;
        }
        // 根据服务名称获取服务
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("service-product");
        if (CollectionUtils.isEmpty(serviceInstances)){
            return null;
        }
        ServiceInstance instance = serviceInstances.get(0);
        sb=new StringBuffer();
        sb.append("http://"+instance.getHost()+":"+instance.getPort()+"/product/list");

        ResponseEntity<List<Product>> response = restTemplate.exchange(sb.toString(), HttpMethod.GET, null, new ParameterizedTypeReference<List<Product>>() {
        });
        return response.getBody();
    }

    /**
     * Ribbon 负载均衡器方式
     * @return
     */
    private List<Product> selectProductListByLoadBalancerClient(){
        StringBuffer sb=null;
        //获取服务名称 获取服务
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        System.out.println(instance);
        if (instance==null){
            return null;
        }
        sb=new StringBuffer();
        sb.append("http://"+instance.getHost()+":"+instance.getPort()+"/product/list");
        System.out.println(sb.toString());
        ResponseEntity<List<Product>> response = restTemplate.exchange(sb.toString(), HttpMethod.GET, null, new ParameterizedTypeReference<List<Product>>() {
        });
        return response.getBody();
    }

    /**
     * 注解方式
     * @return
     */
    private List<Product> selectProductListByLoadBalancerAnnotation(){
        ResponseEntity<List<Product>> response = restTemplate.exchange(
                "http://service-product/product/list", HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {
        });
        return response.getBody();
    }
}
